package com.yfp.client.redisDelay;

import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.yfp.client.domain.MallOrder;
import com.yfp.client.domain.OrderCombo;
import com.yfp.client.service.MallOrderService;
import com.yfp.client.service.OrderService;
import com.yfp.client.service.PackOrderService;
import com.yfp.common.core.constant.CacheConstants;
import com.yfp.common.redis.utils.RedisDelayQueueUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.redisson.api.RedissonClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisDelayQueueRunner implements CommandLineRunner {

    @Resource
    private RedissonClient redissonClient;
    @Resource
    private RedisDelayQueueUtil redisDelayQueueUtil;
    @Resource
    private OrderService orderService;
    @Resource
    private PackOrderService packOrderService;
    @Resource
    private MallOrderService mallOrderService;

    @Override
    public void run(String... args) {
        ScheduledThreadPoolExecutor executorService = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), new BasicThreadFactory.Builder().namingPattern("redisDelay-schedule-pool-%d").daemon(true).build());
        executorService.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                while (true) {
                    try {
                        // 处理套餐
                        Object msg = redisDelayQueueUtil.getDelayQueue(CacheConstants.MINAPP_ORDER_CREATE_DELAY_QUEUE_KEY);
                        Gson gson = new Gson();
                        OrderCombo orderCombo = gson.fromJson(msg.toString(), OrderCombo.class);
                        log.info("Redis延迟队列触发，请及时处理您的业务。msg={}", orderCombo);
                        if (Objects.nonNull(orderCombo)) {

                            log.info("订单超时未支付，请及时处理。orderId={}", orderCombo.getOrderId());
                            orderService.cancelOrder(orderCombo.getOrderId(), "超时未支付");
                        }
                    } catch (InterruptedException e) {
                        log.error("(Redis延迟队列异常中断) {}", e.getMessage());
                    }
                }
            }
        }, 0, 1, TimeUnit.SECONDS);// 每秒检测一次
        executorService.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Object msg = redisDelayQueueUtil.getDelayQueue(CacheConstants.MINAPP_ORDER_PACK_CREATE_DELAY_QUEUE_KEY);
                        OrderCombo orderCombo = JSONUtil.toBean(msg.toString(), OrderCombo.class);
                        log.info("Redis延迟队列触发，请及时处理您的业务，礼包。msg={}", orderCombo);
                        if (Objects.nonNull(orderCombo)) {
                            log.info("订单超时未支付，请及时处理，礼包。orderId={}", orderCombo.getOrderId());
                            packOrderService.cancelOrder(orderCombo.getOrderId(), "超时未支付");
                        }
                    } catch (InterruptedException e) {
                        log.error("(Redis延迟队列异常中断)，礼包 {}", e.getMessage());
                    }
                }
            }
        }, 0, 1, TimeUnit.SECONDS);// 每秒检测一次

        executorService.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Object msg = redisDelayQueueUtil.getDelayQueue(CacheConstants.MINAPP_ORDER_MALLPRODUCT_CREATE_DELAY_QUEUE_KEY);
                        MallOrder mallOrder = JSONUtil.toBean(msg.toString(), MallOrder.class);
                        log.info("Redis延迟队列触发，请及时处理您的业务，商城。msg={}", mallOrder);
                        if (Objects.nonNull(mallOrder)) {
                            log.info("订单超时未支付，请及时处理，商城。mallOrderId={}", mallOrder.getId());
                            mallOrderService.cancelOrder(mallOrder.getId());
                        }
                    } catch (InterruptedException e) {
                        log.error("(Redis延迟队列异常中断)，商城 {}", e.getMessage());
                    }
                }
            }
        }, 0, 1, TimeUnit.SECONDS);// 每秒检测一次

    }
}

